<template>
  <div class="teachers_page">
    <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
      <div style="display:flex;align-items: center;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="用户名称">
            <el-input clearable size="small" v-model="formInline.uNickName" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input clearable size="small" v-model="formInline.cName" placeholder="请输入商品名称"></el-input>
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select clearable size="small" v-model="formInline.state" placeholder="请选择">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '未处理'
                },{
                  value: '2',
                  label: '已处理'
                },]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </div>
      <div>
    </div>
  </div>
  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="订单ID" width="100">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column label="用户昵称">
      <template slot-scope="scope">
        {{ scope.row.uNickName }}
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="商品名称">
      <template slot-scope="scope">
        {{ scope.row.cName }}
      </template>
    </el-table-column>
    <el-table-column label="商品图片" width="121">
      <template slot-scope="scope">
        <img :src="scope.row.cImageUrl" style="width:100px;height:100px" alt="">
      </template>
    </el-table-column>
    <el-table-column label="商品积分" width="100">
      <template slot-scope="scope">
        {{ scope.row.cCost }}
      </template>
    </el-table-column>
    <el-table-column label="换购数量" width="100">
      <template slot-scope="scope">
        {{ scope.row.quantity }}
      </template>
    </el-table-column>
    <el-table-column label="订单状态" width="100">
      <template slot-scope="scope">
        <el-tag type="danger" v-if="scope.row.status == 0">未处理</el-tag>
        <el-tag type="success" v-else>已处理</el-tag>
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="商品描述" width="200">
      <template slot-scope="scope">
        {{ scope.row.cDescription }}
      </template>
    </el-table-column>
    <el-table-column label="联系人姓名" width="100">
      <template slot-scope="scope">
        {{ scope.row.contactName }}
      </template>
    </el-table-column>
    <el-table-column label="联系人电话" width="200">
      <template slot-scope="scope">
        {{ scope.row.contactNumber }}
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="联系人地址" width="200">
      <template slot-scope="scope">
        {{ scope.row.province }}{{ scope.row.city }}{{ scope.row.district }}{{ scope.row.contactAddress }}
      </template>
    </el-table-column>
    <el-table-column label="下单时间" width="200">
      <template slot-scope="scope">
        {{ scope.row.orderTime }}
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="scope">
        <!-- <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm> -->
        <el-button @click="openDetail(scope.row)" type="text">查看</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="openModal(scope.row)" type="text" style="color:#ff0000;">处理订单</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageObj.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="pageObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>

  <el-dialog title="处理订单" :visible.sync="dialogFormVisible">
    <el-form :model="formModal">
      <el-form-item style="margin-bottom:10px;" label="订单状态" :label-width="formLabelWidth">
        <el-select style="width:100%;" size="small" v-model="formModal.state" placeholder="请选择">
          <el-option
            v-for="item in [{
              value: 1,
              label: '未处理'
            },{
              value: 2,
              label: '已处理'
            },]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-bottom:10px;" label="快递单号" :label-width="formLabelWidth">
        <el-input size="small" v-model="formModal.trackingNumber" placeholder="请输入快递单号"></el-input>
      </el-form-item>
      <el-form-item label="备注信息" :label-width="formLabelWidth">
        <el-input type="textarea" :rows="5" size="small" v-model="formModal.adminRemark" autocomplete="off" placeholder="请输入备注信息"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelModal">取 消</el-button>
      <el-button type="primary" @click="saveModal">确 定</el-button>
    </div>
  </el-dialog>


  <el-dialog width="70%" title="查看订单" :visible.sync="detailModal">
    <el-form>
      <el-form-item label="订单ID" :label-width="formLabelWidth">
        <div>
          {{detailObj.id}}
        </div>
      </el-form-item>
      <el-form-item label="商品图片" :label-width="formLabelWidth">
        <img :src="detailObj.cImageUrl" style="width:100px;height:100px" alt="">
      </el-form-item>
      <el-form-item label="商品名称" :label-width="formLabelWidth">
        <div>
          {{detailObj.cName}}
        </div>
      </el-form-item>
      <el-form-item label="商品积分" :label-width="formLabelWidth">
        <div>
          {{detailObj.cCost}}
        </div>
      </el-form-item>
      <el-form-item label="换购数量" :label-width="formLabelWidth">
        <div>
          {{detailObj.quantity}}
        </div>
      </el-form-item>
      <el-form-item label="订单状态" :label-width="formLabelWidth">
        <div>
          <el-tag type="danger" v-if="detailObj.state == 1">未处理</el-tag>
          <el-tag type="success" v-else>已处理</el-tag>
        </div>
      </el-form-item>
      <el-form-item label="商品描述" :label-width="formLabelWidth">
        <div>
          {{detailObj.cDescription}}
        </div>
      </el-form-item>
      <el-form-item label="用户昵称" :label-width="formLabelWidth">
        <div>
          {{detailObj.uNickName}}
        </div>
      </el-form-item>
      <el-form-item label="联系人姓名" :label-width="formLabelWidth">
        <div>
          {{detailObj.contactNumber}}
        </div>
      </el-form-item>
      <el-form-item label="联系人电话" :label-width="formLabelWidth">
        <div>
          {{detailObj.contactNumber}}
        </div>
      </el-form-item>
      <el-form-item label="联系人地址" :label-width="formLabelWidth">
        <div>
          {{ detailObj.province }}{{ detailObj.city }}{{ detailObj.district }}{{ detailObj.contactAddress }}
        </div>
      </el-form-item>
      <el-form-item label="下单时间" :label-width="formLabelWidth">
        <div>
          {{detailObj.orderTime}}
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align:center;">
      <el-button @click="detailModal = false">关 闭</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {
  getOrderListApi,
  editOrderApi,
  getOrderDetailApi,
} from '@/api/repurchase'

export default {
  data() {
    return {
      list: [],
      listLoading: false,
      currentPage: 1,
      formInline: {
        uNickName: '',
        cName: '',
        state: '',
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      formLabelWidth: '100px',
      formModal: {
        state: '',
        adminRemark: '',
        trackingNumber: '',
      },
      dialogFormVisible: false,
      orderObj: {},
      detailObj: {},
      detailModal: false,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    openDetail(row) {
      getOrderDetailApi(row.id).then((res) => {
        this.detailModal = true
        this.detailObj = res.data
      })
    },
    cancelModal() {
      this.dialogFormVisible = false
      this.formModal.state = ''
      this.formModal.adminRemark = ''
      this.formModal.trackingNumber = ''
    },
    saveModal() {
      editOrderApi({
        id: this.orderObj.id,
        status: this.formModal.state,
        adminRemark: this.formModal.adminRemark,
        trackingNumber: this.formModal.trackingNumber,
      }).then((res) => {
        this.$message.success('操作成功！')
        this.cancelModal()
        this.fetchData()
      })
    },
    openModal(item) {
      this.orderObj = item
      this.formModal.state = item.status
      this.formModal.adminRemark = item.adminRemark
      this.formModal.trackingNumber = item.trackingNumber
      this.dialogFormVisible = true
    },
    onSubmit() {
      this.fetchData()
    },
    reset() {
      this.formInline.uNickName = ''
      this.formInline.cName = ''
      this.formInline.state = ''
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.fetchData()
    },
    edit(row) {
      this.$router.push({
        path: '/teachers/edit',
        query: {
          editId: row.id
        }
      })
    },
    fetchData() {
      this.listLoading = true
      getOrderListApi({
        uNickName: this.formInline.uNickName,
        cName: this.formInline.cName,
        status: this.formInline.state,
        pageNum: this.pageObj.pageNum,
        pageSize: this.pageObj.pageSize,
      }).then(response => {
        this.list = response.data.result
        this.listLoading = false
        this.pageObj.total = response.data.total
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style>
  .el-table .cell{
    white-space: pre-line;
  }
</style>

<style lang="scss" scoped>
   .teachers_page{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
